package bb.lanxing.network;

import com.baidu.mapapi.model.LatLng;
//import com.igexin.push.p244f.C5359r;
import gov.nist.core.Separators;
import bb.lanxing.common.config.Constants;
import bb.lanxing.util.Log;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class ThirdPartyClient {
    final private static String TAG = "ThirdPartyClient";
    public static OkHttpClient client;

    static {
        client = new OkHttpClient();
        client = new OkHttpClient.Builder().connectTimeout(20L, TimeUnit.SECONDS).build();
    }

    public static String buildUrlByParams(String url, HashMap<String, Object> params) {
        StringBuilder sb = new StringBuilder();
        sb.append(url);
        if (params != null) {
            Iterator<String> it2 = params.keySet().iterator();
            if (it2.hasNext()) {
                sb.append(Separators.QUESTION);
            }
            while (it2.hasNext()) {
                try {
                    String next = it2.next();
                    Object obj = params.get(next);
                    sb.append(next);
                    sb.append("=");
                    if ("mcode".equals(next)) {
                        sb.append(obj.toString());
                    } else {
                        sb.append(URLEncoder.encode(obj.toString(), "utf-8"));
                    }
                    sb.append("&");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    public static Call requestWeatherData(LatLng latLng) {
        HashMap hashMap = new HashMap();
        hashMap.put("appid", Constants.OPEN_WEATHER_MAP_APP_ID);
        hashMap.put("lat", latLng.latitude);
        hashMap.put("lon", latLng.longitude);
        hashMap.put("units", "metric");
        hashMap.put("lang", "zh_cn");
        Request build = new Request.Builder().addHeader("Accept", "application/json").url(buildUrlByParams("http://api.openweathermap.org/data/2.5/weather", hashMap)).build();
        Log.d(TAG, build.toString());
        return client.newCall(build);
    }
}
